using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class MoveObject : MonoBehaviour
{
    public float moveTime = .1f;
    public LayerMask blockingLayer;

    private BoxCollider2D boxCollider;
    private Rigidbody rb2D;
    private float inverseMoveTime;

    protected virtual void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
        rb2D = GetComponent<Rigidbody>();
        inverseMoveTime = 1f / moveTime;
    }

    protected bool Move(int xDir, int yDir, out RaycastHit2D hit)
    {
        Vector2 start=transform.position;
        Vector2 end=start + new Vector2(xDir,yDir);

        boxCollider.enabled = false;
        hit=Physics2D.Linecast(start,end,blockingLayer);
        boxCollider.enabled=true;

        if(hit.transform==null)
        {
            StartCoroutine(SmoothMovement(end));
            return true;
        }
        return false;
    }

    protected IEnumerator SmoothMovement(Vector3 end)
    {
        float sqrRemainingDistance=(transform.position-end).sqrMagnitude;
        while(sqrRemainingDistance>float.Epsilon)
        {
            Vector3 newPosition =Vector3.MoveTowards(rb2D.position,end,inverseMoveTime*Time.deltaTime);
            rb2D.MovePosition(newPosition);
            sqrRemainingDistance=(transform.position-end).sqrMagnitude;
            yield return null;
        }
    }
    protected virtual void AttemptMove(int xDir,int yDir)
        
    {
        RaycastHit2D hit;
        bool canMove=Move(xDir,yDir,out hit);
        if(hit.transform==null)
            return;

    }

    protected abstract void OnCantMove<T>(T component)
        where T : Component;
    
}
